﻿/*我的相册*/

//初始化js引用
document.write('<script src="/JS/swfUploadImg/swfupload.js" type="text/javascript"></script>');
document.write('<script src="/JS/swfUploadImg/plugins/swfupload.speed.js" type="text/javascript"></script>');
document.write('<script src="/JS/SYHUpload.js" type="text/javascript"></script>');

///图片插入委托接口对象
var galleryDelegte = {

    submitImage: function () {

        var result = "";
        var oldimgUrl = "";
        if ($("#picQueue img").length > 0) {
            result += "[";
            $("#picQueue img").each(function () {

                oldimgUrl = $(this).attr("src").split('_')[0] + $(this).attr("exteson");
                result += "{'url':'" + $(this).attr("src") + "','imgid':'" + $(this).attr("imgid") + "','exteson':'" + $(this).attr("exteson") + "','original':'"+oldimgUrl+"'},"

            })

            var index = result.lastIndexOf(',');
            result = result.substring(0, index);
            result += "]";
        }
        this.callBack(result);
        $("#picQueue").html("");

    },
    callBack: function (json) {


    },
    dialogNum: ''
}
 


///获取图片分页数据
function getPicPager(gid,currentIndex){

    $.get("/User/Images/GetImageList?alumsId=" + gid + "&page=" + currentIndex, function (result) {
        $("#div_picList").html(result);

    });

}

//选择相册时图片列表数据绑定
function bindDdlChangeEvent() {

    $("#ddlalums").live("change", function () {

        var id = $(this).val();
        getPicPager(id, 1);
    })

}

//选项卡(我的相册和上传图片)切换
function changetab(v) {
    if (v == 2) {
        $(".tab1").hide();
        $(".tab2").show();

    } else {

        $(".tab2").hide();
        $(".tab1").show();

    }
}

//初始化图片上传控件
function invalidUpload() {
    syhUpload.getInstance({
        uploadUrl: "/User/Images/ImageUpload?alumsId=" + $("#ddlalums").val() + "&m=" + Math.random(),
        placeHoldId: "uploadBtn",
        file_size_limit: "5 MB",
        file_queue_limit: 4,
        fileQueued: function (file) {
            var fileName = file.name;
            if (fileName.indexOf(",") >= 0) {
                return;
            }
            $("#divprogresscontainer").show();

            var content = $("<div class='lod'><span class='fd-left'>" + fileName + "</span><div class='loding'><div class='lodingbg' style='width:0%' id='div_progress" + file.index + "'></div></div><span class='fl' id='uppercent" + file.index + "'>( 等待上传中... )</span><div>")

            $("#divprogresscontainer").append(content);
        },
        uploadSuccess: function (file, serverData) {
            debugger;
            var uppercent = $("#uppercent" + file.index);
            uppercent.addClass("red1");
            uppercent.html("上传成功！");
            InsertFile(serverData, file.name);

        },
        fileDialogComplete: function (numFilesSelected, numFilesQueued, numFilesInQueue) {
            this.startUpload();
        },
        fileQueueError: function (file, errorCode, message) {
            var errorMessage = "";
            switch (errorCode) {
                case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                    errorMessage = "每次仅能上传4个文件!";
                    break;
                case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                    errorMessage = "每个文件不能超过5 MB!";
                    break;
                case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                default:
                    errorMessage = "请选择正确的文件格式!";
                    break;

            }
            art.dialog({ title: "提示", icon: "error", content: errorMessage });
        },
        uploadProgress: function (file, bytesLoaded, bytesTotal) {
            var percent = Math.ceil((bytesLoaded / bytesTotal) * 100) + "%";

            var thisProgress = $("#div_progress" + file.index);
            if (thisProgress) {
                thisProgress.css("width", percent);
            }
            var thisPercent = $("#uppercent" + file.index)
            if (thisPercent) {
                thisPercent.html(percent);
            }

        }
    }, true)


}

//初始化数据绑定
function invalidData() {

    var currentAblumnId = $("#ddlalums").val();
    getPicPager(currentAblumnId, 1); //分页数据
    bindDdlChangeEvent();//绑定相册下拉框 

}


function selectpic(obj) {

    var img = $(obj).find("img");
    var url = img.attr("src");
    var imgID = img.attr("imgId");
    var exteson = img.attr("lang");
    var insertcount = $("#picQueue").children().length;
    if (insertcount < 4) {
        $("#picQueue").append("<li><span class=\"img1\" onclick=\"$(this).parent().remove();\"><a href=\"###\"></a></span><a href=\"###\"><img src=\"" + url + "\" imgid=\"" + imgID + "\"   exteson=\"" + exteson + "\"/></a></li>");
    }

}




///打开弹窗
function openUploadView(dialogNum) {
    galleryDelegte.dialogNum = dialogNum;
     jQuery.dialogHelper.open(
        "/User/Images/Index",
        { id: 'ablumid',top:'2%',lock:true },
       function () {
           invalidUpload();
           invalidData();
       });
  
}




///打开新建相册输入框
function showAblumb() {

    var ishidden = $("#ablumIdCreate").is(":hidden");
    if (ishidden) {
        $("#ablumIdCreate").show();
    }
    else {
        $("#ablumIdCreate").hide();
    }
}

//创建相册
function createAblum() {

    var ablumname=$("#txtablumName").val();
    $.post("/User/Images/CreateAblum", { 'name': ablumname }, function (data) {

        if (data == "-1") {
            jQuery.dialogHelper.alert("请输入相册名称!", "alert");
        }
        else if (data == "0") {

            jQuery.dialogHelper.alert("操作失败!", "alert");

        }
        else {
            jQuery.dialogHelper.alert("创建成功!", "success");
            $("#ddlablum2,#ddlalums").append("<option value='" + data + "'>" + ablumname + "</option>"); //刷新相册下拉框
            $("#ddlablum2,#ddlalums").attr("value", data); //刷新相册下拉框
          
        }

    })

}

//插入图片
function InsertFile(url, ext) {

    var alumsId = $("#ddlalums").val();
    $.ajax({
        type: "GET",
        url: "/User/Images/InsertImage",
        data: "action=callback&imageinfo=" + url + "&alumsId=" + alumsId + "&ext=" + ext,
        success: function (imgid) {
            $("#picQueue").append("<li class=\"ihover\"><span onclick=\"$(this).parent().remove();\" class=\"img1\"><a class=\"removeImage\" href=\"###\"></a></span><a href=\"###\"><img src=\"" + url + "\"   imgid=\"" + imgid + "\"   exteson=\"" + ext + "\" /></a></li>");
            
        }
    });

}


///提交已选择的图片
function submitImage(callBack) {
    //判断是否选择图片
//    if ($("#picQueue li img").length <= 0) {
//        art.dialog({
//            content: "请选择要插入的图片!",
//            time: 2
//        });
//        return;
//    }

//    var imgList = "";
//    $("#picQueue li img").each(function () {

//        imgList += "<img src='" + $(this).attr("src") + "' imgid='" + $(this).attr("exteson") + "'/>";
//    })
//    alert(imgList);

//    parent.editor.execCommand('insertHtml', imgList);                     
 
}



